#include "analyse.h"

int i = 1;
int tag = 0;
GetFile get;
GetMD5 md5;
void analyse::analyse_one(const QString &source, const QString &destination, Ui::MainWindow *ui, int flag)
{
    QFileInfoList fileInfoList = get.getAllFile(source);
    QFileInfoList fileInfoList2 = get.getAllFile(destination);
    QList<QString> dirList = get.getDir(source);
    QList<QString> dirList2 = get.getDir(destination);
    if (flag == 1 || flag == 2) {
        fileInfoList2 = rightfile(fileInfoList, fileInfoList2,ui,source,destination,flag);
        rightdir(dirList, ui,source,destination,  flag);
        leftfile(fileInfoList2, ui,source, destination, flag);
        leftdir(dirList2,ui,source, destination, flag);
        i = 1;
    }
    if (flag == 0) {
        leftfile(fileInfoList2, ui,source, destination, flag);
        leftdir(dirList2,ui,source, destination, flag);
        rightfile(fileInfoList, fileInfoList2,ui,source,destination,flag);
        rightdir(dirList, ui,source,destination,  flag);
        i = 1;
    }
    if (tag) {
        //QMessageBox::warning(NULL, "Warning","The IP Address Format is not right, please Input again!");
        /*QMessageBox messagebox(NULL);
        //messagebox.setWindowIcon(QIcon(":/Icon/warning.png"));
        messagebox.setWindowTitle("Warning");
        messagebox.setStyleSheet(
                "QLabel { font:18px;}"
            );
        messagebox.setText("You have conflicts that have not been handled yet! Please right-click the conflict icon to make a selection!");
        messagebox.addButton("Okay", QMessageBox::AcceptRole);
        messagebox.addButton("Cancel", QMessageBox::RejectRole);
        messagebox.exec();*/
        QMessageBox message(QMessageBox::Warning,"Warning!!!","<font size='26' color='red'>You have conflicts that have not been handled yet! Please right-click the conflict icon to make a selection!</font>", QMessageBox::Yes | QMessageBox::No, NULL);
        message.exec();
        tag = 0;
    }

}

void analyse::rightdir(QList<QString> dirList, Ui::MainWindow *ui,const QString &source, const QString &destination, int flag)
{
    foreach (QString str, dirList) {
        QDir dir(str);
        QString str3 = str;
        QString str2 = str.replace(source, destination);
        QFileInfoList fileList = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files, QDir::Time);
        if (!QDir(str2).exists()) {
            QTreeWidgetItem *newltem = new QTreeWidgetItem();
            newltem->setText(0,QString::number(i++));
            newltem->setText(1, str3.remove(source));
            newltem->setIcon(1, QIcon(":/Icon/dir.png"));
            newltem->setText(2, QString::number(4096));
            if (!dir.isEmpty())
                newltem->setText(3, fileList.first().lastRead().toString());
            if (flag == 1 || flag == 2)
                newltem->setIcon(4, QIcon(":/Icon/right-plus.png"));
            if (flag == 0)
                newltem->setIcon(4, QIcon(":/Icon/right-del.png"));
            newltem->setBackgroundColor(1, qRgb(177,177,177));
            ui->treeWidget->addTopLevelItem(newltem);
        }
    }
}

QFileInfoList analyse::rightfile(QFileInfoList fileInfoList, QFileInfoList fileInfoList2, Ui::MainWindow *ui,const QString &source, const QString &destination, int flag)
{

    foreach (QFileInfo info, fileInfoList) {

        QString newpath = info.absoluteFilePath();
        QString newpath3 = newpath;
        QString newpath4 = newpath;
        QString newpath2 = newpath.replace(source, destination);
        if (!QFile(newpath2).exists() || QString::compare(md5.getFileMd5(newpath4),md5.getFileMd5(newpath2))) {
            QTreeWidgetItem *newltem = new QTreeWidgetItem();
            newltem->setText(0,QString::number(i++));
            newltem->setText(1,newpath3.remove(source));
            newltem->setIcon(1,QIcon(":/Icon/file.png"));
            newltem->setText(2,QString::number(info.size()));
            newltem->setText(3,info.lastRead().toString());
            if (flag == 1 || (flag == 2 && !QFile(newpath2).exists()))
                newltem->setIcon(4, QIcon(":/Icon/right-plus.png"));
            if (flag == 0)
                newltem->setIcon(4, QIcon(":/Icon/right-del.png"));
            if (flag == 2 && QFile(newpath2).exists()) {
                fileInfoList2.removeOne(QFileInfo(newpath2));
                newltem->setTextColor(0, Qt::red);
                newltem->setTextColor(1, Qt::red);
                newltem->setTextColor(2, Qt::red);
                newltem->setTextColor(3, Qt::red);
                newltem->setTextColor(7, Qt::red);
                newltem->setTextColor(8, Qt::red);
                newltem->setTextColor(9, Qt::red);
                tag = 1;
                newltem->setIcon(5, QIcon(":/Icon/conflict.png"));
                newltem->setText(7,newpath3.remove(source));
                newltem->setIcon(7,QIcon(":/Icon/file.png"));
                newltem->setText(8,QString::number(QFileInfo(newpath2).size()));
                newltem->setText(9,QFileInfo(newpath2).lastRead().toString());
            }
            ui->treeWidget->addTopLevelItem(newltem);
        }
    }
    return fileInfoList2;
}
void analyse::leftdir(QList<QString> dirList2, Ui::MainWindow *ui,const QString &source, const QString &destination, int flag)
{
    foreach (QString str, dirList2) {
        QDir dir(str);
        QString str3 = str;
        QString str2 = str.replace(destination, source);
        QFileInfoList fileList = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files, QDir::Time);
        if (!QDir(str2).exists()) {
            QTreeWidgetItem *newltem = new QTreeWidgetItem();
            newltem->setText(0,QString::number(i++));
            if (flag == 0 || flag == 2)
                newltem->setIcon(6, QIcon(":/Icon/left-plus.png"));
            if (flag == 1)
                newltem->setIcon(6, QIcon(":/Icon/left-del.png"));
            newltem->setText(7, str3.remove(destination));
            newltem->setIcon(7, QIcon(":/Icon/dir.png"));
            newltem->setText(8, QString::number(4096));
            if (!dir.isEmpty())
                newltem->setText(9, fileList.first().lastRead().toString());
            newltem->setBackgroundColor(7, qRgb(177,177,177));
            ui->treeWidget->addTopLevelItem(newltem);
        }
    }
}

void analyse::leftfile(QFileInfoList fileInfoList2, Ui::MainWindow *ui,const QString &source, const QString &destination, int flag)
{
    foreach (QFileInfo info, fileInfoList2) {
        QString newpath = info.absoluteFilePath();
        QString newpath3 = newpath;
        QString newpath4 = newpath;
        QString newpath2 = newpath.replace(destination, source);
        if (!QFile(newpath2).exists() || QString::compare(md5.getFileMd5(newpath4),md5.getFileMd5(newpath2))) {
            QTreeWidgetItem *newltem = new QTreeWidgetItem();
            newltem->setText(0,QString::number(i++));
            if (flag == 0 || (flag == 2 && !QFile(newpath2).exists()))
                newltem->setIcon(6, QIcon(":/Icon/left-plus.png"));
            if (flag == 1)
                newltem->setIcon(6, QIcon(":/Icon/left-del.png"));
            newltem->setText(7,newpath3.remove(destination));
            newltem->setIcon(7,QIcon(":/Icon/file.png"));
            newltem->setText(8,QString::number(info.size()));
            newltem->setText(9,info.lastRead().toString());
            ui->treeWidget->addTopLevelItem(newltem);
        }
    }
}


